<template>
  <div style="padding: 10px;">
    <Row :space="15">
      <Cell>
        <Input v-model="formData.parameterName" placeholder="参数名称" style="width: 100%;" />
      </Cell>
      <Cell>
        <Input v-model="formData.name" placeholder="节点名称" style="width: 100%;" />
      </Cell>
      <Cell>
        <Select v-model="formData.model" :datas="models" keyName="value" titleName="label" placeholder="选择模型"></Select>
      </Cell>
      <Cell>
        <Select v-model="formData.funcCall" :datas="funcs" keyName="value" titleName="label"
          placeholder="函数调用"></Select>
      </Cell>
      <Cell>
        <Cell>
          <span class="link" @click="addMessageLine">添加一行</span>
        </Cell>
        <Row v-for="(item, i) in formData.messages" :key="i" :space="15">
          <Cell>
            <div class="h-panel">
              <div class="h-panel-bar">
                <Select v-model="item.messageType" :datas="messageType" :no-border="true" :deletable="false"></Select>
                <i class="h-split"></i>
                <Select v-model="item.message" :datas="parameters" :no-border="true" :deletable="false"></Select>
                <i class="h-split"></i>
                <span class="h-panel-right link" @click="delMessageLine(i)">删除</span>
              </div>
              <div>
                <Textarea v-model="item.message" :mustMatch="false" type="title" :datas="parameters" placeholder="消息内容"
                  rows="5" style="width: 100%;"></Textarea>
              </div>
            </div>
          </Cell>
        </Row>
      </Cell>
      <Cell>
        <Checkbox v-model="formData.printInput" placeholder="跟踪打印" title="跟踪打印">打印输入</Checkbox>
        <Space :width="15" />
        <Checkbox v-model="formData.printOutput" title="跟踪打印">打印输出</Checkbox>
        <Space :width="15" />
        <Checkbox v-model="formData.inputHistory" title="会话历史">保存输入</Checkbox>
        <Space :width="15" />
        <Checkbox v-model="formData.resultHistory" title="会话历史">保存输出</Checkbox>
      </Cell>
      <Cell>
        <Button color="primary" @click="bindForm">设置</Button>
        <Space :width="15" />
        <Button @click="setForm" color="yellow">重置</Button>
      </Cell>
    </Row>
  </div>
</template>

<script>
import LogicFlow from '@logicflow/core';
import { message } from 'heyui';

export default {
  props: {
    lf: Object,
    currentNode: Object,
  },
  mounted() {
    this.getModel();
    this.getFunctionsDatas();
    this.setForm();
    this.getParameters();
  },
  data() {
    return {
      models: [],
      messageType: [{ label: "系统类型", value: "system" }, { label: "用户类型", value: "human" }, { label: "AI类型", value: "ai" }],
      funcs: [],
      parameters: [],
      formData: {
        id: "",
        name: "",
        model: "",
        funcCall: "",
        resultHistory: true,
        messages: [{ messageType: "system", message: "" }, { messageType: "human", message: "" }],
      }
    }
  },
  methods: {
    setForm() {
      this.formData.name = this.$props.currentNode.data.text.value;
      let id = this.$props.currentNode.data.id;
      let properties = this.$props.lf.getProperties(id);
      this.formData.id = this.$props.currentNode.data.id;
      this.formData.model = properties.model;
      this.formData.parameterName = properties.parameterName;
      this.formData.printInput = properties.printInput;
      this.formData.printOutput = properties.printOutput;
      this.formData.funcCall = properties.funcCall;
      this.formData.inputHistory = properties.inputHistory;
      if (!!properties.resultHistory) {
        this.formData.resultHistory = properties.resultHistory;
      }
      if (!!properties.messages) {
        this.formData.messages = properties.messages;
      }
    },
    bindForm() {
      let id = this.$props.currentNode.data.id;
      this.$props.lf.updateText(id, this.formData.name);
      this.$props.lf.setProperties(id, this.formData);
      this.$emit('close', false);
      message.success("设置成功");
    },
    addMessageLine() {
      this.formData.messages.push({ messageType: "human", message: "", })
    },
    delMessageLine(index) {
      this.formData.messages.splice(index, 1);
    },
    getFunctionsDatas() {
      this.$post("/ollamafunctions/list", { page: { current: 1, size: 1000 } }).then(res => {
        if (res.data.code == this.$success) {
          this.funcs = res.data.data.rows;
        }
      });
    },
    getModel() {
      this.$get("/chat/getModel").then(res => {
        if (res.data.code == this.$success) {
          this.models = res.data.data;
        } else {
          message.error(res.data.msg);
        }
      });
    },

    getParameters() {
      let flowData = (this.$props.lf).getGraphRawData();
      let nodes = flowData["nodes"];
      let node = this.getStartNode(flowData);
      while (!!node && node?.id != this.$props.currentNode?.data?.id && node?.type != 'EndNode') {
        var properties = node?.properties;
        var name = properties?.name;
        var parameterName = properties?.parameterName;

        if (!!parameterName) {
          if (properties?.inputHistory == true) {
            this.parameters.push({ label: "$" + parameterName + ".input", value: "$" + parameterName + ".input" });
          } if (properties?.resultHistory == true) {
            this.parameters.push({ label: "$" + parameterName + ".output", value: "$" + parameterName + ".output" });
          }
        }
        node = this.getNextNode(flowData, node.id);
      }
    },
    getStartNode(flowData) {
      let nodes = flowData["nodes"];
      for (var i = 0; i < nodes.length; i++) {
        let node = nodes[i];
        if (node.type == 'StartNode') {
          return node;
        }
      }
    },
    getNode(flowData, id) {
      let nodes = flowData["nodes"];
      for (var i = 0; i < nodes.length; i++) {
        let node = nodes[i];
        if (node.id == id) {
          return node;
        }
      }
    },
    getNextNode(flowData, id) {
      let edges = flowData["edges"]
      for (var i = 0; i < edges.length; i++) {
        let edge = edges[i];
        if (edge.sourceNodeId == id) {
          return this.getNode(flowData, edge.targetNodeId);
        }
      }
    },
  }
}
</script>